package com.mzj.saas.rent;

import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.dto.Node;
import com.haixiaoke.saas.repository.pojo.MaterialGroup;
import com.haixiaoke.saas.repository.service.IMaterialGroupService;
import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.annotation.RepeatSubmit;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 素材组Controller
 * 
 * @author panda
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/material/group")
public class MaterialGroupController extends BaseController {
    @Autowired
    private IMaterialGroupService materialGroupService;
    @Autowired
    LoginUserUtil loginUserUtil;

    /**
     * 查询素材组列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MaterialGroup materialGroup) {
        startPage();
        List<MaterialGroup> list = materialGroupService.selectMaterialGroupList(materialGroup);
        return getDataTable(list);
    }

    /**
     * 查询该租户下的素材组树状结构
     * @param materialGroup 查询条件
     * @return  树状结构
     */
    @GetMapping("/tree/list")
    @CheckAuth
    public List<Node> treeList(MaterialGroup materialGroup){
        materialGroup.setOrgId(loginUserUtil.getOrgId());
        return materialGroupService.selectGroupTreeList(materialGroup);
    }

    /**
     * 导出素材组列表
     */
/*    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialGroup materialGroup) {
        List<MaterialGroup> list = materialGroupService.selectMaterialGroupList(materialGroup);
        ExcelUtil<MaterialGroup> util = new ExcelUtil<MaterialGroup>(MaterialGroup.class);
        util.exportExcel(response, list, "素材组数据");
    }*/

    /**
     * 获取素材组详细信息
     */
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
        return success(materialGroupService.selectMaterialGroupByGroupId(groupId));
    }

    /**
     * 新增素材组
     */
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody MaterialGroup materialGroup) {
        materialGroup.setOrgId(loginUserUtil.getOrgId());
        materialGroup.setCreateBy(loginUserUtil.getName());
        return toAjax(materialGroupService.insertMaterialGroup(materialGroup));
    }

    /**
     * 修改素材组
     */
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody MaterialGroup materialGroup) {
        materialGroup.setUpdateBy(loginUserUtil.getName());
        return toAjax(materialGroupService.updateMaterialGroup(materialGroup));
    }

    /**
     * 删除素材组
     */
	@DeleteMapping("/{groupIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        if (groupIds == null || groupIds.length == 0) {
            return error("请选择数据");
        }
        MaterialGroup group = new MaterialGroup();
        group.setUpdateTime(DateUtil.date());
        group.setUpdateBy(loginUserUtil.getName());
        group.setGroupIds(groupIds);
        return toAjax(materialGroupService.softDeleteMaterialGroupByGroupIds(group));
    }
}
